package com.wcs.exam.controller.admin.biz;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.wcs.exam.common.base.BaseBiz;
import com.wcs.exam.common.base.Result;
import com.wcs.exam.common.base.page.Page;
import com.wcs.exam.common.base.page.PageUtil;
import com.wcs.exam.controller.admin.req.AdminSysLogPageReq;
import com.wcs.exam.controller.admin.resp.AdminSysLogPageResp;
import com.wcs.exam.dao.SysLogDao;
import com.wcs.exam.dao.SysUserDao;
import com.wcs.exam.dao.impl.mapper.entity.SysLog;
import com.wcs.exam.dao.impl.mapper.entity.SysUser;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Admin-后台操作日志表
 *
 * @author wcs
 * @since 2024-07-06
 */
@Component
@RequiredArgsConstructor
public class AdminSysLogBiz extends BaseBiz {

    @NotNull
    private final SysLogDao dao;

    @NotNull
    private final SysUserDao sysUserDao;


    /**
     * 后台操作日志表-分页
     *
     * @param req 后台操作日志表-分页查询参数
     * @return 后台操作日志表
     */
    public Result<Page<AdminSysLogPageResp>> page(AdminSysLogPageReq req) {
        Map<Long, SysUser> userMap = new HashMap<>();
        if (StrUtil.isNotBlank(req.getRealName()) || StrUtil.isNotBlank(req.getMobile())) {
            List<SysUser> userList = sysUserDao.lambdaQuery()
                    .like(StrUtil.isNotBlank(req.getRealName()), SysUser::getRealName, req.getRealName())
                    .like(StrUtil.isNotBlank(req.getMobile()), SysUser::getMobile, req.getMobile())
                    .list();
            if (CollectionUtil.isEmpty(userList)) {
                return Result.success(new Page<>());
            }
            userList.forEach(item -> userMap.put(item.getId(), item));
        }

        // 处理查询条件
        LambdaQueryWrapper<SysLog> wrapper = new LambdaQueryWrapper<SysLog>()
                .in(CollUtil.isNotEmpty(userMap), SysLog::getUserId, userMap.keySet())
                .eq(ObjectUtil.isNotNull(req.getUserId()), SysLog::getUserId, req.getUserId())
                .like(StrUtil.isNotBlank(req.getOperation()), SysLog::getOperation, req.getOperation())
                .like(StrUtil.isNotBlank(req.getMethod()), SysLog::getMethod, req.getMethod())
                .like(StrUtil.isNotBlank(req.getPath()), SysLog::getPath, req.getPath())
                .like(StrUtil.isNotBlank(req.getContent()), SysLog::getContent, req.getContent())
                .like(StrUtil.isNotBlank(req.getLoginIp()), SysLog::getLoginIp, req.getLoginIp())
                .like(StrUtil.isNotBlank(req.getCountry()), SysLog::getCountry, req.getCountry())
                .like(StrUtil.isNotBlank(req.getProvince()), SysLog::getProvince, req.getProvince())
                .like(StrUtil.isNotBlank(req.getCity()), SysLog::getCity, req.getCity())
                .like(StrUtil.isNotBlank(req.getBrowser()), SysLog::getBrowser, req.getBrowser())
                .like(StrUtil.isNotBlank(req.getOs()), SysLog::getOs, req.getOs())
                // 排序
                .orderByDesc(SysLog::getId);
        // 分页查询
        IPage<SysLog> page = dao.page(PageDTO.of(req.getPageCurrent(), req.getPageSize()), wrapper);
        // 处理分页响应信息
        Page<AdminSysLogPageResp> respPage = PageUtil.transform(page, AdminSysLogPageResp.class);
        if (ObjectUtil.isNull(respPage) || CollectionUtil.isEmpty(respPage.getList())) {
            return Result.success(respPage);
        }


        if (CollectionUtil.isEmpty(userMap)) {
            List<Long> userIds = respPage.getList().stream().map(AdminSysLogPageResp::getUserId).collect(Collectors.toList());
            List<SysUser> userList = sysUserDao.listByIds(userIds);
            if (CollectionUtil.isNotEmpty(userList)) {
                userList.forEach(item -> userMap.put(item.getId(), item));
            }
        }
        for (AdminSysLogPageResp resp : respPage.getList()) {
            SysUser sysUser = userMap.get(resp.getUserId());
            if (ObjectUtil.isNotNull(sysUser)) {
                resp.setRealName(sysUser.getRealName());
                resp.setMobile(sysUser.getMobile());
            }
        }
        return Result.success(respPage);
    }

}
